package com.woniuxy.contract.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.woniuxy.commons.annotation.RequirePerms;
import com.woniuxy.commons.entity.Contract;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.stafflog.LogRequirePerms;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.contract.service.ContractService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xyq
 * @create 2021-11-10 11:05
 */
@RestController
@RequestMapping("/contract")
public class ContractController {

    @Resource
    private ContractService contractService;

    //添加合同的方法
    @RequirePerms("user:add")
    @LogRequirePerms(module = "",description = "添加合同信息")
    @PutMapping("/addContract")
    @SentinelResource(value = "add",fallback = "fallbackaddContract")
    public ResponseResult<Contract> logaddContract(@RequestBody Contract contract){
        contractService.addContract(contract);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("添加成功");
        return responseResult;
    }

    //添加的降级方法
    public ResponseResult<Contract> fallbackaddContract(){
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(500);
        responseResult.setMessage("系统维护中");
        return responseResult;
    }


    //删除合同的方法
    @LogRequirePerms(module = "",description = "删除合同信息")
    @RequirePerms("user:del")
    @DeleteMapping("/delContract/{conid}")
    public ResponseResult<Contract> logdelContract(@PathVariable("conid") int conid){
        contractService.delectContractByid(conid);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("删除成功");
        return responseResult;
    }


    //修改合同的方法
    @LogRequirePerms(module = "",description = "修改合同信息")
    @RequirePerms("user:update")
    @PutMapping("/updateContract")
    public ResponseResult<Contract> logupdateContract(@RequestBody Contract contract){
        contractService.updateContract(contract);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("修改成功");
        return responseResult;

    }


    //得到所有的合同方法(分页)
    @LogRequirePerms(module = "",description = "查询所有合同信息")
    @RequirePerms("user:select")
    @GetMapping("/getallContract")
    @SentinelResource(value = "get",fallback = "fallbackgetallContract")
    public ResponseResult<PageBean<Contract>> loggetAllContract(Contract contract, int pageIndex, int pageSize){

        System.out.println(contract);

        List<Contract> contracts = contractService.selectAllContractByPage(contract, pageIndex, pageSize);
        int i = contractService.selectContractCountByPage(contract);
        PageBean<Contract> pageBean=new PageBean<>();
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(i);
        pageBean.setBeanList(contracts);
        ResponseResult<PageBean<Contract>> responseResult=new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("查询成功");
        responseResult.setData(pageBean);
        return responseResult;
    }

    //查询所有的降级的方法
    public ResponseResult<Contract> fallbackgetallContract(Contract contract,int pageIndex,int pageSize){
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(500);
        responseResult.setMessage("系统维护中");
        return responseResult;
    }




}
